package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.course.CourseDTO;
import com.qk.entity.Course;
import com.qk.management.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 *
 * @author MrZhao
 * @created 2025-10-12 22:48
 * @description 课程管理Controller
 *
 */
@Slf4j
@RestController
@RequestMapping(path = "/courses")
@SuppressWarnings("all")
public class CourseController {

    @Autowired
    private CourseService courseService;

    // 7. 根据学科查询课程
    @GetMapping("/subject/{subjectId}")
    public Result listBySubject(@PathVariable Integer subjectId){
        return Result.success(courseService.listBySubject(subjectId));
    }


    // 6. 查询所有课程
    @GetMapping("/list")
    public Result list(){
        return Result.success(courseService.list());
    }


    // 5. 修改课程
    @PutMapping
    public Result update(@RequestBody CourseDTO dto){
        log.info("修改课程：{}", dto);
        courseService.updateById(dto);
        return Result.success();
    }

    //4.根据id查询课程
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("查询课程id:{}", id);
        return Result.success(courseService.getById(id));
    }

    //2. 删除课程
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("删除课程：{}", id);
        courseService.deleteById(id);
        return Result.success();
    }


    //3.添加课程
    @PostMapping
    public Result add(@RequestBody CourseDTO dto){
        log.info("添加课程：{}", dto);
        courseService.add(dto);
        return Result.success();
    }

    //1. 课程列表查询
    @GetMapping
    public Result page(CourseDTO dto){
        PageResult<Course> result = courseService.page(dto);
       return Result.success(result);
    }

}
